package com.mission.schedule.my160920.adapter;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.applcation.App;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.my160920.FrendLyActivity;
import com.mission.schedule.my160920.NewEndTaskActivity;
import com.mission.schedule.my160920.NewMyFragment;
import com.mission.schedule.my160920.bean.FrendLyBean;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by liwei on 16/9/21.
 */
public class FrendLyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mcontent;
    private List<FrendLyActivity.ListLyBean> frendLyBeanList;


    public FrendLyAdapter(Context context, List<FrendLyActivity.ListLyBean> frendLyBeanList) {
        this.mcontent = context;
        this.frendLyBeanList = frendLyBeanList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = new ViewHolder(
                LayoutInflater.from(mcontent).inflate(R.layout.new_my_ly_item, parent, false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showStubImage(R.mipmap.img_null_smal)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.img_null_smal)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.img_null_smal)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)                          // 设置下载的图片是否缓存在SD卡中
//                        .displayer(new RoundedBitmapDisplayer(20))  // 设置成圆角图片
                .build();

        String imageurl = URLConstants.HOST+"/TbDU_showImage.htm?imageName=" + frendLyBeanList.get(position).getU_PORTRAIT() + "&imageType=2&imageSizeType=3";

        ViewHolder viewHolder = (ViewHolder) holder;

        ImageLoader.getInstance().displayImage(imageurl,viewHolder.imageView,options);
        viewHolder.name.setText(frendLyBeanList.get(position).getU_NICK_NAME());
        viewHolder.time.setText(frendLyBeanList.get(position).getChangeTime().replace("T"," ").substring(0,frendLyBeanList.get(position).getChangeTime().length()-3));
        viewHolder.message.setText(frendLyBeanList.get(position).getMess());
        viewHolder.ly_item_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alterImportantDialog(frendLyBeanList.get(position).getMess());
            }
        });

    }

    @Override
    public int getItemCount() {
        return frendLyBeanList.size();
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView name,time,message;
        LinearLayout ly_item_ll;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView)itemView.findViewById(R.id.lys_icon);
            name = (TextView)itemView.findViewById(R.id.lys_name);
            time = (TextView)itemView.findViewById(R.id.lys_time);
            message = (TextView)itemView.findViewById(R.id.lys_message);
            ly_item_ll = (LinearLayout) itemView.findViewById(R.id.ly_item_ll);

        }
    }

    /**
     * 点击留言
     */
    private void alterImportantDialog(String message) {
        Dialog dialog = new Dialog(mcontent, R.style.dialog_translucent_adjustPan);
        Window window = dialog.getWindow();
        android.view.WindowManager.LayoutParams params = window.getAttributes();// 获取LayoutParams
        params.alpha = 0.92f;
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        window.setAttributes(params);// 设置生效

        LayoutInflater fac = LayoutInflater.from(mcontent);
        View more_pop_menu = fac.inflate(R.layout.add_reply_dialog, null);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setContentView(more_pop_menu);
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.show();

        new alterImportantDialogOnClick(dialog, more_pop_menu,message);
    }

    class alterImportantDialogOnClick implements View.OnClickListener {

        private View view;
        private Dialog dialog;
        private TextView add_tv;
        private TextView copy_tv;
        private TextView canel_tv;
        private String message;

        @SuppressLint("NewApi")
        public alterImportantDialogOnClick(Dialog dialog, View view,String message) {
            this.dialog = dialog;
            this.view = view;
            this.message = message;
            initview();
        }

        private void initview() {
            add_tv = (TextView) view.findViewById(R.id.add_tv);
            add_tv.setOnClickListener(this);
            copy_tv = (TextView) view.findViewById(R.id.copy_tv);
            copy_tv.setOnClickListener(this);
            canel_tv = (TextView) view.findViewById(R.id.canel_tv);
            canel_tv.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.add_tv:
                    if (App.getDBcApplication().queryFriendCommonReply().size() < 21){
                        App.getDBcApplication().insertFriendCommonReply(message);
                        Toast.makeText(mcontent, "已添加常用回复", Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(mcontent, "请先删除部分常用回复再进行添加", Toast.LENGTH_SHORT).show();
                    }

//                    if(App.getDBcApplication().checkMessage(message) == true){

//                    }else {
//                        Toast.makeText(mcontent, "常用回复中已存在", Toast.LENGTH_SHORT).show();
//                    }

                    dialog.dismiss();
                    break;
                case R.id.copy_tv:
                    // 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager，虽然提示deprecated，但不影响使用。
                    ClipboardManager cm = (ClipboardManager) mcontent.getSystemService(Context.CLIPBOARD_SERVICE);
                    // 将文本内容放到系统剪贴板里。
                    cm.setText(message);
                    Toast.makeText(mcontent, "已复制", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                    break;
                case R.id.canel_tv:
                    dialog.dismiss();
                    break;
            }
        }
    }
}
